<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code Igniter
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package		CodeIgniter
 * @author		Gabriel Popa
 * @copyright	Copyright (c) 2008, IELLO-DEV
 * @license		http://www.codeignitor.com/user_guide/license.html
 * @link		http://www.codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

class Interface_bill {
	
	function Interface_bill(){
		$this->CI =& get_instance();
		$this->CI->load->model("m_common","common");
		log_message('debug', "Bill library initialized");
	}
	function build($array,$action_type){
 		//common values
		$sess 			= $this->CI->session->userdata('us_');
 		$cId 			= $sess['us_C'];
 		$user 			= $sess['us_N'];
 		$userCNP		= $sess['us_S'];
		$userId			= $sess['us_I'];
		
		//default values
		$articles 		= array();
		$totalValoare 	= 0;
		$totalTVA 		= 0;
		$facturat 		= 0;		
		
		//constructed values
		$userData	= array(
							'utilizator_id' 	=>$userId,
							'utilizator_cnp'	=>$userCNP,
							'utilizator_nume'	=>$user
		);		
		
		$rowCode	= $this->CI->common->mSelectBillCode($cId);
		$company 	= $this->CI->common->mSelectCompany($cId);
		$client		= $this->CI->common->mSelectWhere('clienti',$array['client']);
		$moneda		= $this->CI->common->mSelectReferenceById($array['moneda']);
		$stampila 	= $this->CI->common->mSelectReferenceById($array['stampila']);
		$mt 		= (isset($array['nr_auto']) && !empty($array['nr_auto'])) ? 'AUTO' : '';		
		$nr_auto 	= (isset($array['nr_auto']) && !empty($array['nr_auto'])) ? $array['nr_auto'] : '';
		$nr_contract= (isset($array['nr_contract']) && !empty($array['nr_contract'])) ? $array['nr_contract'] : '';
		$detalii	= (isset($array['detalii']) && !empty($array['detalii'])) ? $array['detalii'] : '';
		$articles	= json_decode($array['articole'],true);
		$rowTVA		= $this->CI->common->mSelectTVAPayMode($array['client']);

		switch($rowTVA->value){
			case 'DA':
				$TVA = 19;
				break;
			case 'NU':
				$TVA = 0;
				break;
			default:
				$TVA = 19;
				break;		
		}		
						
		switch ($action_type){
			case 'save':
				$facturat 		= 1;
				//completing $articles array with calculated values valoare and tva and then sumerizing
				$data = array(
							'bill' 	=>array(
												'numar'				=> $rowCode->numar,
												'seria'				=> $rowCode->seria,
												'FK_CLIENTI' 		=> $array['client'],
												'data'				=> $array['data'],									
												'nr_contract'		=> $nr_contract,
												'data_emiterii'		=> $array['data_emiterii'],
												'data_scadentei'	=> $array['data_scadentei'],
												'detalii'			=> $detalii,
												'delegat'			=> json_encode(array("name" => $array['delegat'], "data" =>  $array['delegat_ci'])),
												'nr_auto'			=> $nr_auto,
												'FK_UTILIZATORI'	=> $userData['utilizator_id'],
												'facturat'			=> $facturat,
												'moneda'			=> $array['moneda'],
												'stampila'			=> $array['stampila'],
												'COMPANY_ID'		=> $cId		
											),
							'tva'		=>	$TVA												
				);
				return $data;
				break;
			case 'view':
				//---------------------------
				//completing $articles array with calculated values valoare and tva and then sumerizing
				foreach ($articles as $key => &$value) {
					foreach ($value as $k => $v){
						//foreach value select produs data
						$produs	= $this->CI->common->mSelectWhere('produse',$value['FK_PRODUSE']);
						
						$value['produs']	= $produs;
						$value['valoare'] 	= round(($value['cantitate']*$value['pret_unitar']),2);
						$value['tva'] 		= round(($TVA/100) * $value['valoare'],2);
					}
					$totalValoare += $value['valoare'];
					$totalTVA += $value['tva'];
				}
				/**
				 *  after we added another field - identity data for delegat, we saved those data toghether into a json string. So we need to check
				 *  if data is from old database record, or a new saved one
				*/
				if ( (substr($array['delegat'], -1) == "}") && (substr($array['delegat'], 0, 1) == "{")){
					$arrDelegat = json_decode($array['delegat'],true);
					$delegat = $arrDelegat['name'];	
					$delegat_ci = $arrDelegat['data'];					
				} else {
					$delegat = $array['delegat'];
					$delegat_ci = '';
				}
				
				$rowUser = $this->CI->common->mSelectUserById($array['FK_UTILIZATORI']);
				$DBuserData	= array(
									'utilizator_id' 	=>$rowUser->ID,
									'utilizator_cnp'	=>$rowUser->cnp,
									'utilizator_nume'	=>$rowUser->name." ".$rowUser->surname
				);				

				$bill = array(
										'compania'			=>$company,							//it's me
										'numar'				=>$array['numar'],
										'seria'				=>$rowCode->seria,
										'client'			=>$client,							//it's them
										'data'				=>$array['data'],
										'nr_contract'		=>$nr_contract,
										'data_emiterii'		=>$array['data_emiterii'],
										'data_scadentei'	=>$array['data_scadentei'],
										'detalii'			=>$detalii,
										'delegat'			=>$delegat,
										'delegat_ci'		=>$delegat_ci,
										'nr_auto'			=>$nr_auto,
										'FK_UTILIZATORI'	=>$DBuserData,
										'facturat'			=>$facturat,
										'moneda'			=>$moneda->value,
										'tva'				=>$TVA,
										'stampila'			=>$stampila->value,
										'COMPANY_ID'		=>$cId,
										'articole'			=>$articles,
										'mijloc_transport'	=>$mt,
										'nr_auto'			=>$nr_auto,
										'TOTAL_VAL'			=>$totalValoare,
										'TOTAL_TVA'			=>$totalTVA,
										'TOTAL'				=>$totalValoare + $totalTVA
				);
				return $bill;			
				break;
			case 'preview':
				//---------------------------
				//completing $articles array with calculated values valoare and tva and then sumerizing
				foreach ($articles as $key => &$value) {
					foreach ($value as $k => $v){
						//foreach value select produs data
						$produs	= $this->CI->common->mSelectWhere('produse',$value['FK_PRODUSE']);
						
						$value['produs']	= $produs;
						$value['valoare'] 	= round(($value['cantitate']*$value['pret_unitar']),2);
						$value['tva'] 		= round(($TVA/100) * $value['valoare'],2);
					}
					$totalValoare += $value['valoare'];
					$totalTVA += $value['tva'];
				}
						
				$bill = array(
										'compania'			=>$company,							//it's me
										'numar'				=>$rowCode->numar,
										'seria'				=>$rowCode->seria,
										'client'			=>$client,							//it's them
										'data'				=>$array['data'],
										'nr_contract'		=>$nr_contract,
										'data_emiterii'		=>$array['data_emiterii'],
										'data_scadentei'	=>$array['data_scadentei'],
										'detalii'			=>$detalii,
										'delegat'			=>$array['delegat'],
										'delegat_ci'		=>$array['delegat_ci'],
										'nr_auto'			=>$nr_auto,
										'FK_UTILIZATORI'	=>$userData,
										'facturat'			=>$facturat,
										'moneda'			=>$moneda->value,
										'tva'				=>$TVA,
										'stampila'			=>$stampila->value,
										'COMPANY_ID'		=>$cId,
										'articole'			=>$articles,
										'mijloc_transport'	=>$mt,
										'nr_auto'			=>$nr_auto,
										'TOTAL_VAL'			=>$totalValoare,
										'TOTAL_TVA'			=>$totalTVA,
										'TOTAL'				=>$totalValoare + $totalTVA
				);
				return $bill;				
				break;
		}


	}
}		
?>
